package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.common.MsgResult;
import com.itheima.common.R;
import com.itheima.dao.TbContractDao;
import com.itheima.dto.ContractQueryDto;
import com.itheima.mapper.SysUserMapper;
import com.itheima.pojo.TbContract;
import com.itheima.service.TbContractService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * (TbContract)表服务实现类
 */
@Service("tbContractService")
@Slf4j
public class TbContractServiceImpl implements TbContractService {
    @Autowired
    private TbContractDao tbContractDao;

    //合同分页查询列表展示接口
    @Override
    public R listContract(ContractQueryDto contractQueryDto) {
        IPage<TbContract> p = new Page<>(contractQueryDto.getPageNum(),contractQueryDto.getPageSize());
        LambdaQueryWrapper<TbContract> wrapper= new LambdaQueryWrapper<>();
        if (contractQueryDto.getContractNo()!=null){
            wrapper.like(TbContract::getContractNo,contractQueryDto.getContractNo());  //合同编号
        }
        if (contractQueryDto.getName()!=null){
            wrapper.like(TbContract::getName,contractQueryDto.getName());  //客户姓名
        }
        if (contractQueryDto.getPhone()!=null){
            wrapper.like(TbContract::getPhone,contractQueryDto.getPhone());  //手机号
        }
        if (contractQueryDto.getSubject()!=null){
            wrapper.like(TbContract::getSubject,contractQueryDto.getSubject());  //购买学科
        }
        if (contractQueryDto.getCourseId()!=null){
            wrapper.like(TbContract::getCourseId,contractQueryDto.getCourseId());  //购买课程的课程id
        }
        if (contractQueryDto.getBeginCreateTime()!=null&&contractQueryDto.getEndCreateTime()!=null){
            wrapper.between(TbContract::getCreateTime,contractQueryDto.getBeginCreateTime(),contractQueryDto.getEndCreateTime()); //创建时间
        }
        IPage<TbContract> tbContractIPage = tbContractDao.selectPage(p, wrapper);
        return new R(1, MsgResult.SUCCESS_FIND,tbContractIPage);
    }

    //新增合同接口
    @Override
    public R add(TbContract tbContract,String username){
        //补全数据
        log.info("创建人=="+username);
        tbContract.setCreateBy("admin");
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy.MM.dd-HH.mm.ss");
        Date date = null;
        try {
            date = sdf.parse(sdf.format(new Date()));
        } catch (ParseException e) { }
        tbContract.setCreateTime(new Date());
        tbContract.setStatus("1");
        //todo 文件名称 文件上传后自动返回
        //添加进数据库
        tbContractDao.insert(tbContract);
        return new R(1,MsgResult.SUCCESS_ADD,tbContract);
    }

    //查看合同详细接口
    @Override
    public R detailsContract(String id) {
        TbContract tbContract = tbContractDao.selectById(id);
        if (tbContract!=null){
            return new R(1,MsgResult.SUCCESS_FIND,tbContract);
        }
        return new R(0,MsgResult.WRONG_FIND,null);
    }

    //合同审核--审核通过或驳回接口
    @Override
    public R auditContract(String id, String status) {
        TbContract tbContract = tbContractDao.selectById(id);
        //todo 根据登录人的权限判断是否有审核资格
        tbContract.setStatus(status);
        tbContractDao.updateById(tbContract);
        return new R(1,MsgResult.SUCCESS_MODIFY,tbContract);
    }

}
